package com.fr.data.impl.sap;

import com.fr.general.ComparatorUtils;
import com.fr.general.data.DataModel;
import com.fr.script.Calculator;
import com.fr.stable.ArrayUtils;
import com.fr.stable.ParameterProvider;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomFunctionSAPTableData extends SAPTableData {
    private String bapiName;
    private String[] inputValue = null;
    private String choosedTable;
    private List list1 = new ArrayList();
    private List list2 = new ArrayList();

    public String getBapiName() {
        return this.bapiName;
    }

    public void setBapiName(String bapiName) {
        this.bapiName = bapiName;
    }

    public String getChoosedTable() {
        return this.choosedTable;
    }

    public void setChoosedTable(String choosedTable) {
        this.choosedTable = choosedTable;
    }

    public int getSAPInputCount() {
        return list1.size();
    }

    public SAPInput[] getSAPInput() {
        return (SAPInput[]) list1.toArray(new SAPInput[list1.size()]);
    }

    public Object[] getSAPInputConfig(int index) {
        SAPInput input = (SAPInput) list1.get(index);
        Object[] objs = new Object[7];
        objs[0] = input.getNo();
        objs[1] = input.getParaName();
        objs[2] = input.getType();
        objs[3] = input.getReturnName();
        objs[4] = input.getDataType();
        objs[5] = input.getCorresPara();
        if (!ArrayUtils.isEmpty(this.parameters)) {
            for (int i = 0; i < this.parameters.length; i++) {
                if (this.parameters[i] != null && ComparatorUtils.equals(objs[5], this.parameters[i].getName())) {
                    objs[6] = this.parameters[i].getValue();
                    break;
                }
            }
        } else {
            objs[6] = null;
        }

        return objs;
    }

    public void setSAPInput(SAPInput[] inputs) {
        list1.clear();
        for (int i = 0; i < inputs.length; i++) {
            list1.add(inputs[i]);
        }
    }

    public SAPOutput[] getSAPOutput() {
        return (SAPOutput[]) list2.toArray(new SAPOutput[list2.size()]);
    }

    public void setSAPOutput(SAPOutput[] outputs) {
        list2.clear();
        for (int i = 0; i < outputs.length; i++) {
            list2.add(outputs[i]);
        }
    }

    public DataModel createDataModel(Calculator calculator) {
        String[] colNames = new String[list2.size()];
        for (int i = 0; i < colNames.length; i++) {
            colNames[i] = ((SAPOutput) list2.get(i)).getColName();
        }

        ParameterProvider[] ps = processParameters(calculator);
        Map<String, Object> psMap = new HashMap<String, Object>();
        for (int i = 0; i < ps.length; i++) {
            psMap.put(ps[i].getName(), ps[i].getValue());
        }

        Map inMap = new HashMap();
        SAPInput in;
        for (int i = 0; i < list1.size(); i++) {
            in = (SAPInput) list1.get(i);
            inMap.put(in.getParaName(), psMap.get(in.getCorresPara()));
        }

        return new CustomFunctionSAPDataModel(connectionName, bapiName, choosedTable, colNames, inMap);
    }

    public void readXML(XMLableReader reader) {
        super.readXML(reader);
        if (reader.isChildNode()) {
            String tmpVal;
            if (reader.getTagName().equals("CFSAPTableDataAttr")) {
                if ((tmpVal = reader.getAttrAsString("bapiName", null)) != null) {
                    this.setBapiName(tmpVal);
                }
//                if ((tmpVal = reader.getAttrAsString("bapiTablesName", null)) != null) {
//                    this.setBapiTablesName(tmpVal.split(","));
//                }
                if ((tmpVal = reader.getAttrAsString("choosedTable", null)) != null) {
                    this.setChoosedTable(tmpVal);
                }
            } else if ("SAPOutput".equals(reader.getTagName())) {
                SAPOutput i = new SAPOutput();
                i.readXML(reader);
                list2.add(i);
            } else if ("SAPInput".equals(reader.getTagName())) {
                SAPInput i = new SAPInput();
                i.readXML(reader);
                list1.add(i);
            }
        }
    }

    public void writeXML(XMLPrintWriter writer) {
        super.writeXML(writer);
        writer.startTAG("CFSAPTableDataAttr");
        if (this.bapiName != null) {
            writer.attr("bapiName", this.getBapiName());
        }
//        if (this.bapiTablesName != null) {
//            writer.attr("bapiTablesName", getFormatString(this.bapiTablesName));
//        }
        if (this.choosedTable != null) {
            writer.attr("choosedTable", this.getChoosedTable());
        }
        writer.end();
        for (int i = 0; i < list1.size(); i++) {
            ((SAPInput) list1.get(i)).writeXML(writer);
        }
        for (int i = 0; i < list2.size(); i++) {
            ((SAPOutput) list2.get(i)).writeXML(writer);
        }
    }

    public Object clone() throws CloneNotSupportedException {
        CustomFunctionSAPTableData cloned = (CustomFunctionSAPTableData) super.clone();

        if (this.inputValue != null) {
            cloned.inputValue = new String[this.inputValue.length];
            for (int i = 0; i < this.inputValue.length; i++) {
                cloned.inputValue[i] = this.inputValue[i];
            }
        }

//		if (this.bapiTablesName != null) {
//			cloned.bapiTablesName = new String[this.bapiTablesName.length];
//			for (int i = 0; i < this.bapiTablesName.length; i++) {
//				cloned.bapiTablesName[i] = this.bapiTablesName[i];
//			}
//		}

        if (this.list1 != null) {
            cloned.list1 = new ArrayList();
            for (int i = 0; i < this.list1.size(); i++) {
                cloned.list1.add(((SAPInput) this.list1.get(i)).clone());
            }
        }

        if (this.list2 != null) {
            cloned.list2 = new ArrayList();
            for (int i = 0; i < this.list2.size(); i++) {
                cloned.list2.add(((SAPOutput) this.list2.get(i)).clone());
            }
        }

        return cloned;
    }
}
